package com.scy.controller;

import com.scy.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vm")
public class VMController {
    int i=0;
    static byte[] bytes1= new byte[1024*1024*100];//200m

    @RequestMapping("/test")
    @ResponseBody
    public User test() {

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.MINUTES.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
        ).start();

        return new User("zs","sh");
    }


    @RequestMapping("/getvmpara")
    @ResponseBody
    public User getVMParam() {
        System.out.println("-------getVMParam方法执行了------");
        long maxMemory = Runtime.getRuntime().maxMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();

        if (i < 1) {
            //第一次访问
            System.out.println("第一次访问该方法");
            // 开启shell脚本
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            byte[] bytes = new byte[1024 * 1024 * 50];
                            Runtime.getRuntime().exec("sh /root/my1.sh");
                            System.out.println("执行shell脚本完成");
                            Thread.sleep(1*1000);
                            System.out.println(bytes.length);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

            // 向目标文件写内存状态
            new Thread(new Runnable() {
                String path = "/root/vm_output";
                @Override
                public void run() {
                    FileOutputStream fos=null;
                    try {
                        fos = new FileOutputStream(path,true);
                        while (true) {
                            long maxMemory1 = Runtime.getRuntime().maxMemory();
                            long totalMemory1 = Runtime.getRuntime().totalMemory();
                            long freeMemory1 = Runtime.getRuntime().freeMemory();

                            String result = "maxMemory:" + maxMemory1 + "----totalMemory:" + totalMemory1 + "----freeMemory:" + freeMemory1;
                            fos.write(result.getBytes(StandardCharsets.UTF_8));
                            String line_separator = System.getProperty("line.separator");
                            fos.write(line_separator.getBytes(StandardCharsets.UTF_8));
//                            fos.flush();
                            Thread.sleep(500);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (fos != null) {
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
            i = i +1;
        }
        //返回内存状态
        return new User("maxMemory:" + maxMemory + "----totalMemory:" + totalMemory,"freeMemory:" + freeMemory);
    }
}
